package com.mjk.common.core.jdbc.service.plus.provider;


import com.mjk.common.base.error.BizException;
import com.mjk.common.base.model.BaseEntity;
import com.mjk.common.core.jdbc.service.plus.constns.PlusConstns;
import com.mjk.common.core.jdbc.service.plus.provider.impl.example.GBaseExampleProvider;
import com.mjk.common.core.jdbc.service.plus.provider.impl.example.MysqlExampleProvider;
import com.mjk.common.core.jdbc.service.plus.provider.impl.example.OracleExampleProvider;
import com.mjk.common.core.jdbc.service.plus.provider.impl.example.SqlServerExampleProvider;
import com.mjk.common.core.jdbc.service.plus.provider.impl.wrap.*;
import com.mjk.common.core.jdbc.service.plus.provider.impl.wrap.mongodb.MongodbProvider;
import com.mjk.common.core.jdbc.service.plus.provider.interfaces.IBeeProvider;
import com.mjk.common.core.jdbc.service.plus.provider.interfaces.IBeeTypeMapping;
import com.mjk.common.core.jdbc.util.TableUtils;
import com.mjk.common.tools.logger.BizLogger;
import com.mjk.common.util.ObjectUtils;
import lombok.Getter;

/**
 * 不同数据库 对应的sql提供类
 */
public enum BeeDataSourceType {
    POSTGRESQL_WRAP("postgresql", PlusConstns.WRAP,new PostgreSqlWrapProvider()),
    GBASE_WRAP("gbasedbt",PlusConstns.WRAP,new GBaseWrapProvider()),
    MONGODB_WRAP("mongodb",PlusConstns.WRAP,new MongodbProvider()),
    MYSQL_WRAP("mysql",PlusConstns.WRAP,new MysqlWrapProvider()),
    ORACLE_WRAP("oracle",PlusConstns.WRAP,new OracleWrapProvider()),
    DM_WRAP("dm",PlusConstns.WRAP,new OracleWrapProvider()),
    SQLSERVER_WRAP("jtds",PlusConstns.WRAP,new SqlServerWrapProvider()),
    SQLSERVER1_WRAP("sqlserver",PlusConstns.WRAP,new SqlServerWrapProvider()),
    POSTGRESQL_EXAMPLE("postgresql",PlusConstns.WRAP,new PostgreSqlWrapProvider()),
    GBASE_EXAMPLE("gbasedbt",PlusConstns.EXAMPLE,new GBaseExampleProvider()),
    MONGODB_EXAMPLE("mongodb",PlusConstns.EXAMPLE,new MongodbProvider()),
    MYSQL_EXAMPLE("mysql",PlusConstns.EXAMPLE,new MysqlExampleProvider()),
    ORACLE_EXAMPLE("oracle",PlusConstns.EXAMPLE,new OracleExampleProvider()),
    DM_EXAMPLE("dm",PlusConstns.EXAMPLE,new OracleExampleProvider()),
    SQLSERVER_EXAMPLE("jtds",PlusConstns.EXAMPLE,new SqlServerExampleProvider()),
    SQLSERVER1_EXAMPLE("sqlserver",PlusConstns.EXAMPLE,new SqlServerExampleProvider());
    @Getter
    private final String driver;
    /**
     * 类型 warp / example
     */
    private final String type;
    private final IBeeProvider handlerClazz;


    BeeDataSourceType(String driver, String type, IBeeProvider handlerClazz){
        this.driver = driver;
        this.type = type;
        this.handlerClazz = handlerClazz;
    }


    public IBeeProvider getHandlerInstance() {
        return handlerClazz;
    }


    /**
     * 获取数据源类型
     */
    public static BeeDataSourceType getDataSoureType(String type){
        for (BeeDataSourceType c : values()) {
            if (ObjectUtils.isNotEmpty(type) && type.contains(c.getDriver())) {
                return c;
            }
        }
        return null;
    }

    /**
     *
     */
    public static IBeeProvider getHandler(String driver,String type){
        try{
            if(ObjectUtils.isEmpty(driver)) {//如果为空，默认返回mysql
                throw new BizException("driver is empty");
            }
            for (BeeDataSourceType c : values()) {
                if (driver.contains(c.getDriver()) && c.type.equals(type)) {
                    return c.handlerClazz;
                }
            }
        }catch(Exception e){
            BizLogger.error(e);
        }
        return null;
    }
    /**
     * 获取typemapping
     */
    public static IBeeTypeMapping getMapping(BaseEntity entity){
        String type = TableUtils.getDriverName(entity);
        try{
            for (BeeDataSourceType c : values()) {
                if (type.contains(c.getDriver())) {
                    return (IBeeTypeMapping)c.handlerClazz;
                }
            }
        }catch(Exception e){
            BizLogger.error(e);
        }
        return null;
    }

    /**
     * 更具驱动获取
     */
    public static IBeeTypeMapping getMapping(String driver){
        try{
            for (BeeDataSourceType c : values()) {
                if (driver.contains(c.getDriver())) {
                    return (IBeeTypeMapping)c.handlerClazz;
                }
            }
        }catch(Exception e){
            BizLogger.error(e);
        }
        return null;
    }

}
